<?php
namespace app\home\controller;
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/10/13
 * Time: 15:26
 */
vendor('zoujingli.wechat-php-sdk.include');
class Weixin extends \think\Controller
{
    /**
     * 接收微信 发来的消息
     */
    public function getOauth(){
        config('dispatch_success_tmpl','public/success');
        $id = input('param.id/d',0);
        $uri   = input('param.state');
        $wechat = & \Wechat\Loader::get_instance('Oauth', getWeixinConfig());
        $return = $wechat->getOauthAccessToken();
        if($return){//接口验证
           if($uri){
                cookie('wxopenid',$return['openid']);
                if($return['scope']=='snsapi_userinfo'){
                    //添加用户到表
                    $this->addWeixinUser($wechat,$return);
                }
                $this->redirect(base64_decode($uri));
            }else{
                $id && $this->authUser($id,$return,$wechat);
                // $c_id && $this->authCompany($c_id,$return['openid']);
                $msg = '绑定微信成功！';
                return view('public/success',['msg'=>$msg,'url'=>url('Index/index@m')]);
            }

        }else{
            die('权授失败');
        }
    }
    private function getWeixinUserInfo($obj,$data){
        $token  = $data['access_token'];
        $openid = $data['openid'];
        $result = $obj->getOauthUserinfo($token,$openid);
        return $result;
    }

    /**
     * @param $obj
     * @param $data
     * 如果不存在用户则添加
     */
    private function addWeixinUser($obj,$data){
        $result = $obj->getOauthUserinfo($data['access_token'],$data['openid']);
        if($result){
            $obj = db('user');
            if($obj->where(['weixin_openid'=>$result['openid']])->count() == 0){
                $add['username']      = $add['nickname'] = $result['nickname'];
                $add['weixin_openid'] = $result['openid'];
                $add['reg_time']   = time();
                $add['headimgurl'] = $result['headimgurl'];
                $info['address']   = $result['province'].$result['city'];
                $info['sex'] = $result['sex'] == 1 ? '男' : ($result['sex'] == 2 ? '女' : '保密');
                $obj->insert($add);
                $info['uid'] = $obj->getLastInsID();
                db('user_info')->insert($info);
                cookie('userId',$info['uid']);
            }

        }
    }
    /**
     * 保存用户openid
     */
    private function authUser($id,$data,$obj){
        if($id){
            if(strpos($data['scope'],'snsapi_userinfo')!==FALSE){
                $return = $this->getWeixinUserInfo($obj,$data);
                $edit['nickname'] = $return['nickname'];
                $info['sex'] = $return['sex'] == 1 ? '男' : ($return['sex'] == 2 ? '女' : '保密');
                $info['address'] = $return['province '].$return['city'];
                db('user_info')->where(['uid'=>$id])->update($info);
            }
                $edit['weixin_openid'] = $data['openid'];
                db('user')->where('id',$id)->update($edit);
        }
    }

}